package com.GreenIce.android.datastructure;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Scanner;

import com.GreenIce.android.GreenIceActivity;
import com.GreenIce.android.R;
import com.GreenIce.android.datastructure.ConfigData.WorldInfo;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.os.Environment;
import android.util.Log;

public class ConfigHandler {
	
	public static void initData(){
		Activity context = GreenIceActivity.getActivity();
		String packageName = "com.GreenIce.android";
		String path = "/data" 
					  + Environment.getDataDirectory().getAbsolutePath()
					  + "/" + packageName + "/files/config.cfg";//TODO ..路径不一致...
		File dir = new File(path);
		if(!dir.exists()){
			Resources r = context.getResources();
			Properties p = new Properties();
			Scanner sc = new Scanner(r.openRawResource(R.raw.config));
			p.put("starNum", String.valueOf(sc.nextInt()));
			int worldNum = sc.nextInt();
			p.put("worldNum", String.valueOf(worldNum));
			p.put("status", String.valueOf(sc.nextInt()));
			p.put("starSum", String.valueOf(sc.nextInt()));
			p.put("curWorld", String.valueOf(sc.nextInt()));
			p.put("curLevel", String.valueOf(sc.nextInt()));
			p.put("curScore", String.valueOf(sc.nextInt()));
			p.put("curStar", String.valueOf(sc.nextInt()));
			for(int i=0; i<worldNum; i++){
				int levelNum = sc.nextInt();
				p.put("levelNum"+i, String.valueOf(levelNum));
				p.put("playNum"+i, String.valueOf(sc.nextInt()));
				p.put("lockStar"+i, String.valueOf(sc.nextInt()));
				int starSumW = sc.nextInt();
				Log.v("starSumW"+i, ""+starSumW);
				p.put("starSumW"+i, String.valueOf(starSumW));
				for(int j=0; j<levelNum; j++){
					p.put("world"+i+"level"+j, String.valueOf(sc.nextInt()));
				}
			}
			try {
				FileOutputStream fout = context.openFileOutput("config.cfg", Context.MODE_PRIVATE);
				p.store(fout, "");
			} catch (FileNotFoundException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}
	public static void loadData(){
		Activity context = GreenIceActivity.getActivity();
		ConfigData data = ConfigData.getInstance();
		Properties p = new Properties();
		try {
			FileInputStream fin = context.openFileInput("config.cfg");
			p.load(fin);
		} catch (FileNotFoundException e) {
			e.printStackTrace();		
		} catch (IOException e) {
			e.printStackTrace();
		}
		data.starNum = Integer.valueOf(p.getProperty("starNum"));
		data.worldNum = Integer.valueOf(p.getProperty("worldNum"));
		data.status = Integer.valueOf(p.getProperty("status"));
		data.starSum = Integer.valueOf(p.getProperty("starSum"));
		data.curWorld = Integer.valueOf(p.getProperty("curWorld"));
		data.curLevel = Integer.valueOf(p.getProperty("curLevel"));
		data.curScore = Integer.valueOf(p.getProperty("curScore"));
		data.curStar = Integer.valueOf(p.getProperty("curStar"));
		for(int i=0; i<data.worldNum; i++){
			WorldInfo aWorld = new WorldInfo(
					Integer.valueOf(p.getProperty("levelNum"+i)),
					Integer.valueOf(p.getProperty("playNum"+i)),
					Integer.valueOf(p.getProperty("lockStar"+i)),
					Integer.valueOf(p.getProperty("starSumW"+i))
					);
			for(int j=0; j<aWorld.levelNum; j++){
				aWorld.eachLevel[j] = Integer.valueOf(p.getProperty("world"+i+"level"+j));
			}
			data.worldInfo.add(aWorld);
		}
	}
	public static void saveData(){
		Activity context = GreenIceActivity.getActivity();
		ConfigData data = ConfigData.getInstance();
		Properties p = new Properties();
		p.put("starNum", String.valueOf(data.starNum));
		int worldNum = data.worldNum;
		p.put("worldNum", String.valueOf(worldNum));
		p.put("status", String.valueOf(data.status));
		p.put("starSum", String.valueOf(data.starSum));
		p.put("curWorld", String.valueOf(data.curWorld));
		p.put("curLevel", String.valueOf(data.curLevel));
		p.put("curScore", String.valueOf(data.curScore));
		p.put("curStar", String.valueOf(data.curStar));
		for(int i=0; i<worldNum; i++){
			WorldInfo aWorld = data.worldInfo.get(i);
			p.put("levelNum"+i, String.valueOf(aWorld.levelNum));
			p.put("playNum"+i, String.valueOf(aWorld.playNum));
			p.put("lockStar"+i, String.valueOf(aWorld.lockStar));
			p.put("starSumW"+i, String.valueOf(aWorld.starSumW));
			for(int j=0; j<aWorld.levelNum; j++){
				p.put("world"+i+"level"+j, String.valueOf(aWorld.eachLevel[j]));
			}
		}
		try {
			FileOutputStream fout = context.openFileOutput("config.cfg", Context.MODE_PRIVATE);
			p.store(fout, "");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
